﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using System.Drawing;
using System.Net;
using System.Net.NetworkInformation;
using ShadingTab.Properties;
using System.Security.Permissions;
using Microsoft.Win32;
using Microsoft.VisualBasic;
using System.Windows.Forms;
using DotSpatial.Controls.Header;






 
namespace ShadingTab
{
    class UtilityFunctions: Extension
    {
       
        public int getLayerHdl(String LyrName, IMap pvMap)
        {
            for (int i = 0; i < pvMap.Layers.Count; i++)
            {
                if (pvMap.Layers[i].LegendText != null)
                {
                    if (pvMap.Layers[i].LegendText.ToLower() == LyrName.ToLower())
                    { return i; }
                }
            }
            return -1;
        }

        public bool checkLyr(string LayerName, IMap pvMap)
        {
            for (int i = pvMap.Layers.Count - 1; i >= 0; i--)
            {
                int remLyr = getLayerHdl(LayerName, pvMap);
                if (remLyr != -1)
                {
                    return true;
                }
            }
            return false;
        }

        public int getLayerID(string LayerName, IMap pvMap)
        {
            int iLayer = -1;
            int nLayer = pvMap.Layers.Count;
            if (nLayer >= 1)
            {
                for (int i = 0; i < nLayer; i++)
                {
                    if (LayerName == pvMap.Layers[i].LegendText) { iLayer = i; }
                }
            }
            return iLayer;
        }

        public void ClearGraphicMap(IMap pvMap)
        {
            pvMap.MapFrame.DrawingLayers.Clear();
        }

        public void DrawLine(Double x1, Double y1, Double x2, Double y2, Double width, System.Drawing.Color col, IMap MapCanvas)
        {
            Coordinate[] L = new Coordinate[2]; //x-axis

            L[0] = new Coordinate(x1, y1);
            L[1] = new Coordinate(x2, y2);

            LineString ls = new LineString(L);
            //creates a feature from the linestring
            Feature f = new Feature(ls);
            //  Feature f = new Feature(axisLines);
            FeatureSet fs = new FeatureSet(f.FeatureType);
            fs.Features.Add(f);

            MapLineLayer rangeRingAxis;
            rangeRingAxis = new MapLineLayer(fs);// MapPolygonLayer(fs);
            rangeRingAxis.Symbolizer = new LineSymbolizer(col, width);
            MapCanvas.MapFrame.DrawingLayers.Add(rangeRingAxis);


            //MapCanvas.MapFrame.DrawingLayers.Add(rangeRingAxis);

            // Request a redraw
            MapCanvas.MapFrame.Invalidate();
            // MapCanvas.MapFrame.Invalidate();
        }

        public Coordinate getPerpend(Coordinate pt, Coordinate pp1, Coordinate pp2, out double Lenght)
        {
            Coordinate p1 = new Coordinate(pp1.X, pp1.Y);
            Coordinate p2 = new Coordinate(pp2.X, pp2.Y);
            Coordinate closest;
            double L = Distance(pt, p2);

            double x1 = (p1.X - p2.X) * L + p1.X;
            double y1 = (p1.Y - p2.Y) * L + p1.Y;
            p1.X = x1;
            p1.Y = y1;
            double x2 = (p2.X - p1.X) * L + p2.X;
            double y2 = (p2.Y - p1.Y) * L + p2.Y;
            p2.X = x2;
            p2.Y = y2;

            double dx = p2.X - p1.X;
            double dy = p2.Y - p1.Y;

            if ((dx == 0) && (dy == 0))
            {
                // It's a point not a line segment.
                closest = p1;
                dx = pt.X - p1.X;
                dy = pt.Y - p1.Y;
                Lenght = Math.Sqrt(dx * dx + dy * dy);
                return closest;
            }

            // Calculate the t that minimizes the distance.
            double t = ((pt.X - p1.X) * dx + (pt.Y - p1.Y) * dy) / (dx * dx + dy * dy);

            // See if this represents one of the segment's
            // end points or a point in the middle.
            if (t < 0)
            {
                closest = new Coordinate(p1.X, p1.Y);
                dx = pt.X - p1.X;
                dy = pt.Y - p1.Y;
            }
            else if (t > 1)
            {
                closest = new Coordinate(p2.X, p2.Y);
                dx = pt.X - p2.X;
                dy = pt.Y - p2.Y;
            }
            else
            {
                closest = new Coordinate(p1.X + t * dx, p1.Y + t * dy);
                dx = pt.X - closest.X;
                dy = pt.Y - closest.Y;
            }
            Lenght = Math.Sqrt(dx * dx + dy * dy);
            return closest;

        }

        public double Distance(Coordinate pointA, Coordinate pointB)
        {
            double d1 = pointA.X - pointB.X;
            double d2 = pointA.Y - pointB.Y;

            return Math.Sqrt(d1 * d1 + d2 * d2);
        }

        public void kDrawCircle(Coordinate pt, Double r, Int16 numVertex, IMap MapCanvas, Color color)
        {
            Double dAng = 360 / numVertex;
            Coordinate[] cr = new Coordinate[numVertex + 1]; //x-axis
            for (int iAng = 0; iAng <= numVertex; iAng++)
            {
                Double ang1 = iAng * dAng;
                Double x1 = Math.Sin(ang1 * Math.PI / 180) * r + pt.X;
                Double y1 = Math.Cos(ang1 * Math.PI / 180) * r + pt.Y;
                cr[iAng] = new Coordinate(x1, y1);
            }
            LineString ls = new LineString(cr);
            Feature f = new Feature(ls);
            FeatureSet fs = new FeatureSet(f.FeatureType);
            fs.Features.Add(f);

            MapLineLayer circleShp;
            circleShp = new MapLineLayer(fs);
            circleShp.Symbolizer = new LineSymbolizer(color, 1);

            MapCanvas.MapFrame.DrawingLayers.Add(circleShp);

            // Request a redraw
            MapCanvas.MapFrame.Invalidate();
        }

        public void kDrawCircle(Double x0, Double y0, Double r, Int16 numVertex, IMap MapCanvas, Color color)
        {
            Double dAng = 360 / numVertex;
            Coordinate[] cr = new Coordinate[numVertex + 1]; //x-axis
            for (int iAng = 0; iAng <= numVertex; iAng++)
            {
                Double ang1 = iAng * dAng;
                Double x1 = Math.Sin(ang1 * Math.PI / 180) * r + x0;
                Double y1 = Math.Cos(ang1 * Math.PI / 180) * r + y0;
                cr[iAng] = new Coordinate(x1, y1);
            }
            LineString ls = new LineString(cr);
            Feature f = new Feature(ls);
            FeatureSet fs = new FeatureSet(f.FeatureType);
            fs.Features.Add(f);

            MapLineLayer circleShp;
            circleShp = new MapLineLayer(fs);
            circleShp.Symbolizer = new LineSymbolizer(color, 1);

            MapCanvas.MapFrame.DrawingLayers.Add(circleShp);

            // Request a redraw
            MapCanvas.MapFrame.Invalidate();
        }

        public bool IsNumeric(string Text)
        {
            float output;
            return float.TryParse(Text, out output);
        }

        public void removeDupplicateLyr(string LayerName, IMap pvMap)
        {
            for (int i = pvMap.Layers.Count - 1; i >= 0; i--)
            {
                int remLyr = getLayerHdl(LayerName, pvMap);
                if (remLyr != -1)
                {
                    pvMap.Layers.RemoveAt(remLyr);
                }
            }
        }

        public Coordinate circleCoord(double x1, double y1, double x2, double y2, double radius) //function finds coordinates for intersection of line and circle
        {
            double ratio = radius / (Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))); //calculates the ratio of the radius of circle to distance to weather station
            double changex = ratio * (x2 - x1); //distance in x direction to circle
            double changey = ratio * (y2 - y1); //distance in y direction to circle
            double newx = (x1 + changex);
            double newy = (y1 + changey);
            Coordinate newcoord = new Coordinate(newx, newy);
            return newcoord;
        }
    }
}
